package com.jamal2362.apktool.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

public class IconFactory
{
	private float scale;
	private Paint backgroundPaint;
	private Context mContext;
	private int iconsColor;
	private int backgroundColor;
	private boolean isRound;
	private int Alpha;

	public IconFactory(Context c)
	{
		this.mContext = c;
		this.iconsColor = 0;
		this.backgroundColor = Color.DKGRAY;
		this.scale = c.getResources().getDisplayMetrics().density;
		this.backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		this.backgroundPaint.setStyle(Paint.Style.FILL);
		this.isRound = false;
		if (Settings.lightTheme)
			this.Alpha = 255;
		else
			this.Alpha = 200;
	}

	public Bitmap makeIcon(int iconId)
	{
		Drawable drawable = drawableFromVector(mContext, iconId);
		return makeIcon(drawable, 48, 24, 0, this.isRound);
	}

	public Bitmap makeIcon(int background, int iconId)
	{
		this.backgroundColor = background;
		Drawable drawable = drawableFromVector(mContext, iconId);
		return makeIcon(drawable, 48, 24, 0, this.isRound);
	}

	public Bitmap makeIcon(int background, int iconId, int backgroundLen, int iconLen, int padding)
	{
		this.backgroundColor = background;
		Drawable drawable = drawableFromVector(mContext, iconId);
		return makeIcon(drawable, backgroundLen, iconLen, padding, this.isRound);
	}

	public Bitmap makeIcon(Drawable icon)
	{
		return makeIcon(icon, 48, 24, 0, this.isRound);
	}

	public Bitmap makeIcon(int background, Drawable icon)
	{
		this.backgroundColor = background;
		return makeIcon(icon, 48, 24, 0, this.isRound);
	}

	public Bitmap makeIcon(int background, Drawable icon, int backgroundLen, int iconLen, int padding)
	{
		this.backgroundColor = background;
		return makeIcon(icon, backgroundLen, iconLen, padding, this.isRound);
	}

	public Bitmap makeIcon(Drawable drawable, int backgroundLen, int iconLen, int padding, boolean round)
	{
		if (this.backgroundColor != Color.TRANSPARENT)
			this.backgroundPaint.setShadowLayer(2.5f, 1.0f, 1.0f, 0x50000000);
		drawable.setAlpha(this.Alpha);
		int rawBackgroundLen = toInt(backgroundLen * this.scale + 0.5f);
        int rawIconLen = toInt(iconLen * this.scale);
        int rawPadding = toInt(padding * this.scale + 0.5f);
        int rawImageLen = rawBackgroundLen + rawPadding + rawPadding;
        Bitmap image = Bitmap.createBitmap(rawImageLen, rawImageLen, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(image);
        int offset = toInt((rawImageLen - rawIconLen) / 2);
		int iconSize = toInt(offset + rawIconLen);
        drawable.setBounds(offset, offset, iconSize, iconSize);
        synchronized (this)
		{
            this.backgroundPaint.setColor(this.backgroundColor);
			if (round)
			{
				int xy = rawImageLen / 2;
				canvas.drawCircle(xy, xy, rawBackgroundLen / 2, this.backgroundPaint);
			}
			else
			{
				float rp = rawBackgroundLen / 5;
				final RectF rect = new RectF();
				rect.set(0, 0, rawBackgroundLen, rawBackgroundLen);
				canvas.drawRoundRect(rect, rp, rp, this.backgroundPaint);
			}
            drawable.draw(canvas);
        }
        return image;
	}

	public Drawable drawableFromVector(Context context, int drawableId)
	{
		Drawable drawable = ContextCompat.getDrawable(context, drawableId);
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
		{
			drawable = (DrawableCompat.wrap(drawable)).mutate();
		}
		if (this.iconsColor > 0)
			DrawableCompat.setTint(drawable, this.iconsColor);
		return drawable;
	}

	public void setIconsColor(int color)
	{
		this.iconsColor = color;
	}

	public int getIconsColor()
	{
		return iconsColor;
	}

	public void setBackgroungColor(int color)
	{
		this.backgroundColor = color;
	}

	public int getBackgroundColor()
	{
		return backgroundColor;
	}

	public void round(boolean round)
	{
		this.isRound = round;
	}

	public void setAlpha(int alpha)
	{
		this.Alpha = alpha;
	}

	private int toInt(float f)
	{
		return (int)Math.round(f);
	}
}
